<!--********************************************************************
* Copyright© 2000 - 2018 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title data-i18n="resources.title_routeLocatorPoint"></title>
    <style type="text/css">
        body {
            margin: 0;
            overflow: hidden;
            background: #fff;
            width: 100%;
            height: 100%
        }

        #map {
            position: absolute;
            width: 100%;
            height: 100%;
            border: 1px solid #3473b7;
        }

        #toolbar {
            position: absolute;
            top: 50px;
            right: 10px;
            width:400px;
            text-align: center;
            z-index: 100;
            border-radius: 4px;
        }
    </style>
</head>
<body>
<div id="toolbar" class="panel panel-primary">
    <div class='panel-heading'>
        <h5 class='panel-title text-center' data-i18n="resources.text_routeLocatorPoint"></h5></div>
    <div class='panel-body content'>
        <div class='panel'>
            <div class='input-group'>
                <span class='input-group-addon' data-i18n="resources.text_routPoint"></span>
                <input type='number' class='form-control' id='locateMeasure' value='10'/>
            </div>
        </div>
        <input type="button" class="btn btn-default" data-i18n="[value]resources.text_queryRoute"
               style="  margin-bottom: 10px;"
               onclick="CreateRouteObj()"/>
        <input type="button" class="btn btn-default" data-i18n="[value]resources.text_locationPoint"
               style="  margin-bottom: 10px;"
               onclick="RouteLocator()"/>
        <input type="button" class="btn btn-default" data-i18n="[value]resources.text_input_value_clear"
               style="  margin-bottom: 10px;" onclick="Remove()"/>
    </div>
</div>
<div id="map"></div>
<script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
<script type="text/javascript" exclude="iclient-classic" src="../../dist/classic/include-classic.js"></script>
<script>
    var host = window.isLocal ? window.server : "http://support.supermap.com.cn:8090";
    var map, layer, routeObj, pointLayer, vectorLayer, feature,
        lineStyle = {
            strokeColor: "red",
            strokeWidth: 2,
            pointRadius: 3,
            pointerEvents: "visiblePainted",
            fill: false
        },
        pointStyle = {
            fillColor: '#669933',
            fillOpacity: 0.8,
            pointRadius: 5,
            strokeColor: '#aaee77',
            strokeWidth: 3
        },
        url = host + "/iserver/services/map-changchun/rest/maps/长春市区图",
        url2 = host + "/iserver/services/spatialanalyst-changchun/restjsr/spatialanalyst";

    init();

    function init() {
        //初始化图层
        layer = new SuperMap.Layer.TiledDynamicRESTLayer("changchun", url, {
            transparent: true,
            cacheEnabled: true
        }, {maxResolution: "auto"});
        vectorLayer = new SuperMap.Layer.Vector("RouteLayer");
        pointLayer = new SuperMap.Layer.Vector("pointLayer");

        //图层初始化完毕调用添加图层函数
        layer.events.on({"layerInitialized": addLayer});
        map = new SuperMap.Map("map", {
            allOverlays: true, controls: [
                new SuperMap.Control.ScaleLine(),
                new SuperMap.Control.Zoom(),
                new SuperMap.Control.Navigation({
                    dragPanOptions: {
                        enableKinetic: true
                    }
                })]
        });
        map.addControl(new SuperMap.Control.LayerSwitcher(), new SuperMap.Pixel(42, 80));
    }

    //添加必要图层
    function addLayer() {
        map.addLayers([layer, vectorLayer, pointLayer]);
        map.setCenter(new SuperMap.LonLat(4503.6240321526, -3861.911472192499), 1);
    }

    //通过SQL查询路由对象
    function CreateRouteObj() {
        widgets.alert.clearAlert();
        var queryParam, queryBySQLParams, queryBySQLService;
        queryParam = new SuperMap.REST.FilterParameter({
            name: "RouteDT_road@Changchun",
            attributeFilter: "RouteID=1690"
        });
        queryBySQLParams = new SuperMap.REST.QueryBySQLParameters({
            queryParams: [queryParam]
        });
        queryBySQLService = new SuperMap.REST.QueryBySQLService(url, {
            eventListeners: {
                "processCompleted": processCompleted,
                "processFailed": processFailed
            }
        });
        queryBySQLService.processAsync(queryBySQLParams);
    }

    //路由对象查询成功，初始化routeObj的值，同时将查询结果绘制在地图上
    function processCompleted(queryEventArgs) {
        routeObj = geo = queryEventArgs.result.recordsets[0].features[0].geometry;
        var feature = new SuperMap.Feature();
        feature.geometry = geo;
        feature.style = lineStyle;
        vectorLayer.addFeatures(feature);
        widgets.alert.showAlert(resources.msg_queryRouteSuccess, true);
    }

    //路由对象查询失败，显示提示信息
    function processFailed() {
        widgets.alert.showAlert(resources.msg_queryRouteFailure, false);
    }

    //"路由定点"操作，其中将routeObj和里程值作为参数传递到参数对象中
    function RouteLocator() {
        widgets.alert.clearAlert();
        if (feature != null) {
            pointLayer.removeFeatures(feature);
            feature = null;
        }

        if (!routeObj) {
            widgets.alert.showAlert(resources.msg_getRoute, false);
            return;
        }
        var routeLocatorParameters_point = new SuperMap.REST.RouteLocatorParameters({
            "sourceRoute": routeObj,
            "type": "POINT",
            "measure": parseFloat(document.getElementById('locateMeasure').value),
            "offset": 0,
            "isIgnoreGap": true
        });
        var routeLocatorService = new SuperMap.REST.RouteLocatorService(url2,
            {
                eventListeners: {
                    "processCompleted": routeLocatorCompleted,
                    'processFailed': routeLocatorFailed
                }
            }
        )
        routeLocatorService.processAsync(routeLocatorParameters_point);
    }

    //"里程定点"操作成功触发该函数，并显示提示信息
    function routeLocatorCompleted(e) {
        feature = new SuperMap.Feature.Vector();
        var geometry = e.result.resultGeometry;
        feature.geometry = geometry;
        feature.style = pointStyle;
        pointLayer.addFeatures(feature);
        widgets.alert.showAlert(resources.msg_locPointSuccess, true);
    }

    //"里程定点"操作失败触发该函数，并显示提示信息
    function routeLocatorFailed(e) {
        widgets.alert.showAlert(resources.msg_locPointFailed, false);
    }

    //移除要素，并重置里程值
    function Remove() {
        widgets.alert.clearAlert();
        vectorLayer.removeAllFeatures();
        pointLayer.removeAllFeatures();
        //$('#locateMeasure').val('10');
        document.getElementById("locateMeasure").value = "10";
        routeObj = null;
    }

</script>

</body>
</html>